let app = getApp()
Component({
    properties: {
      cartCount:{
        type:Number,
        observer: function(newVal, oldVal) {
          this.update()
        }
      },
      },
      data:{ 
        siteDomain:app.globalData.siteDomain,
        moneySign:'¥',
      },
    lifetimes: {
        attached: function () {
          this.setData({
            getGlobalColor:app.globalData.globalColor
          })
          this.car_data()
       }
      },
    methods: {
      update:function(){
        this.car_data2()
        this.car_data()
      },
      get_goods:function(e){
           var choose=[];
            choose['buy_type']='add';
            choose['buy_num']=e.detail.buy_num;
            choose['goodId']=e.detail.goodId;
            choose['cart_id']=e.detail.cart_id;
            choose['buy_page']='car';
            choose['spec_id_group']=e.detail.spec_id_group;
            this.triggerEvent ('get_goods',choose)
      },
      topay:function(){
        var newspec=this.data.list
        var order=[];
        for(var s=0;s<newspec.length; s++){
          if(newspec[s]['checked']=='checked')
          {
            var chiobj = newspec[s];
              chiobj['id']=newspec[s]['goods_id'],
              chiobj['price']=typeof(newspec[s]['sku_price'])!='undefined'?newspec[s]['sku_price']:newspec[s]['goods_price'],
              chiobj['count']=newspec[s]['num']
              order.push(chiobj)
          }
        }
        wx.setStorageSync('confirmorder',order)
        wx.navigateTo({ 
          url: '/yb_shopv2/native/all/index?type=confirm_order&type_id=0&page_type=carlist',
        })
      },
      car_data:function(){
        let that = this;
        wx.request({
          url: app.globalData.siteroot,
          data: {
            do:'Goods_cartList',
            user_id:wx.getStorageSync('user_id')
          },
          success: function(res) {
                var carlist=res.data.data;
                var count=0;
                var allprice=0;
                for (var i = 0; i <carlist.length; i++) {
                  carlist[i]['checked']='checked'
                  count=parseInt(count)+parseInt(carlist[i]['num']);
                  if(carlist[i]['sku_price']!=''&&carlist[i]['sku_price']!=null)
                  {
                    allprice=allprice+carlist[i]['num']*carlist[i]['sku_price'];
                  }else{
                    allprice=allprice+carlist[i]['num']*carlist[i]['goods_price'];
                  }
               }
               that.setData({
                list:carlist,
                count:count,
                allprice:allprice.toFixed(2)
               })
          }
        })
      },
      nowbuy:function(){
        wx.navigateTo({ 
          url: '/yb_shopv2/native/all/index?type=page_goods_category_level1_style3&pagetitle=商品分类',
       })
      },
      checked:function(e){
        var that=this
         if(e.detail!=1)
         {
          var carlist=this.data.list;
          var count=0;
          var allprice=0;
          for (var i = 0; i <carlist.length; i++) {
            carlist[i]['checked']=''
            count=0;
            allprice=0;
         }
         that.setData({
          list:carlist,
          count:count,
          allprice:allprice.toFixed(2)
         })
         }
         else{
          var carlist=this.data.list;
          var count=0;
          var allprice=0;
          for (var i = 0; i <carlist.length; i++) {
                  carlist[i]['checked']='checked'
                  count=parseInt(count)+parseInt(carlist[i]['num']);
                  if(carlist[i]['sku_price']!=''&&carlist[i]['sku_price']!=null)
                  {
                    allprice=allprice+carlist[i]['num']*carlist[i]['sku_price'];
                  }else{
                    allprice=allprice+carlist[i]['num']*carlist[i]['goods_price'];
                  }
         }
         that.setData({
          list:carlist,
          count:count,
          allprice:allprice.toFixed(2)
         })
         }
      },
      delete_good:function(e){
        this.car_data2()
        this.car_data()
      },
      car_data2:function(){
        let that = this;
        wx.request({
          url: app.globalData.siteroot,
          data: {
            do:'Goods_cartList',
            user_id:wx.getStorageSync('user_id')
          },
          success: function(res) {
                var carlist=res.data.data;
                var count=0;
                var allprice=0;
                for (var i = 0; i <carlist.length; i++) {
                  carlist[i]['checked']='checked'
                  count=parseInt(count)+parseInt(carlist[i]['num']);
                  if(carlist[i]['sku_price']!=''&&carlist[i]['sku_price']!=null)
                  {
                    allprice=allprice+carlist[i]['num']*carlist[i]['sku_price'];
                  }else{
                    allprice=allprice+carlist[i]['num']*carlist[i]['goods_price'];
                  }
               }
               that.setData({
                count:count,
                allprice:allprice.toFixed(2)
               })
          }
        })
      },
      car_list:function(e){
          var that=this
          var carlist=e.detail;
          var count=0;
          var allprice=0;
          var all_c=0;
          for (var i = 0; i <carlist.length; i++) {
            if(carlist[i]['checked']=='checked')
            {
              all_c=parseInt(all_c)+1;
              count=parseInt(count)+parseInt(carlist[i]['num']);
              if(carlist[i]['sku_price']!=''&&carlist[i]['sku_price']!=null)
                  {
                    allprice=allprice+carlist[i]['num']*carlist[i]['sku_price'];
                  }else{
                    allprice=allprice+carlist[i]['num']*carlist[i]['goods_price'];
                  }
            }
         }
         if(all_c==carlist.length)
         {
           that.setData({
            allChecked:1
           })
         }
         else
         {
          that.setData({
            allChecked:0
          })
        
         }
         that.setData({
          list:carlist,
          count:count,
          allprice:allprice.toFixed(2)
         })
      }
    }
});
